<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="84px">
      <el-form-item label="订单号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算状态" prop="settleState">
        <el-select v-model="queryParams.settleState" placeholder="请选择结算状态" clearable size="small">
          <el-option
            v-for="dict in settleStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="平台流水" prop="outTradeNo">
        <el-input
          v-model="queryParams.outTradeNo"
          placeholder="请输入平台支付流水号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通道流水" prop="tradeNo">
        <el-input
          v-model="queryParams.tradeNo"
          placeholder="请输入通道支付流水号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平台结算单" prop="outSettleNo">
        <el-input
          v-model="queryParams.outSettleNo"
          placeholder="请输入平台侧结算单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="抖音分账单" prop="settleNo">
        <el-input
          v-model="queryParams.settleNo"
          placeholder="请输入抖音平台分账单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="gmtTime">
        <el-date-picker
          v-model="dateRange"
          :unlink-panels="unlinkPanels"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="结算周期" prop="settleStart">
        <el-date-picker
          v-model="dateRange"
          :unlink-panels="unlinkPanels"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="toutiaoList" >
      <el-table-column label="订单号" align="center" prop="orderNum" />
      <el-table-column label="结算状态" align="center" prop="settleState" :formatter="settleStateFormat" />
      <el-table-column label="平台支付流水号" align="center" prop="outTradeNo" />
      <el-table-column label="通道支付流水号" align="center" prop="tradeNo" />
      <el-table-column label="平台侧结算单号" align="center" prop="outSettleNo" />
      <el-table-column label="抖音平台分账单号" align="center" prop="settleNo" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="订单金额" align="center" prop="amount" />
      <el-table-column label="结算描述" align="center" prop="settleDesc" />
      <el-table-column label="错误码" align="center" prop="errorNo" />
      <el-table-column label="错误提示" align="center" prop="errTips" />
      <el-table-column label="创建时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算周期" align="center" prop="settleStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.settleStart) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center"  fixed="right"  width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-check"
            @click="handleSettle(scope.row)"
            v-if="scope.row.settleState == 0 || scope.row.settleState == 8"
            v-hasPermi="['pay:toutiao:settle']"
          >结算</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleSettleQuery(scope.row)"
            v-hasPermi="['pay:toutiao:query']"
          >查询</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleRemark(scope.row)"
            v-hasPermi="['pay:toutiao:remark']"
          >备注</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <div>
      <p>错误码：2014 错误描述：已无可分账金额 解决办法：查询是否已有成功或在途分账，或分账前已退款</p>
    </div>

    <el-dialog title="结算备注" :visible.sync="open" width="50%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            placeholder="请输入备注"
            clearable
            size="small"
          />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRemark">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listToutiao, settleToutiao, queryToutiao,settleRemark } from "@/api/order/toutiao";

export default {
  name: "Toutiao",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 头条结算单表格数据
      toutiaoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 结算状态  0 待结算   3 结算中   6 成功  9 失败字典
      settleStateOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: undefined,
        orderNum: undefined,
        settleState: undefined,
        outTradeNo: undefined,
        tradeNo: undefined,
        outSettleNo: undefined,
        settleNo: undefined,
        gmtStartTime: undefined,
        gmtEndTime: undefined,
        settleStartTime: undefined,
        settleEndTime: undefined
      },
      // 表单参数
      form: {
        id:undefined,
        remark: ''
      },
      // 表单校验
      rules: {
      },
      dateRange: [],
      settleDateRange: [],
      unlinkPanels:true,
    };
  },
  created() {
    this.getList();
    this.getDicts("toutiao_settle_state").then(response => {
      this.settleStateOptions = response.data;
    });
  },
  methods: {
    addGmtDateRange(params, dateRange) {
      var search = params;
      search.gmtStartTime = "";
      search.gmtEndTime = "";
      if (null != dateRange && '' != dateRange) {
        search.gmtStartTime = dateRange[0];
        search.gmtEndTime = dateRange[1];
      }
      return search;
    },
    addSettleDateRange(params, dateRange) {
      var search = params;
      search.settleStartTime = "";
      search.settleEndTime = "";
      if (null != dateRange && '' != dateRange) {
        search.settleStartTime = dateRange[0];
        search.settleEndTime = dateRange[1];
      }
      return search;
    },
    /** 查询头条结算单列表 */
    getList() {
      this.loading = true;
      this.addGmtDateRange(this.queryParams,this.dateRange);
      this.addSettleDateRange(this.queryParams,this.finishDateRange);
      listToutiao(this.queryParams).then(response => {
        this.toutiaoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 结算状态  0 待结算   3 结算中   6 成功  9 失败字典翻译
    settleStateFormat(row, column) {
      return this.selectDictLabel(this.settleStateOptions, row.settleState);
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSettleQuery(row){
      queryToutiao(row.id).then(response => {
        this.msgSuccess(response.msg);
        this.getList();
      });
    },
    handleSettle(row){
      settleToutiao(row.id).then(response => {
        this.msgSuccess(response.msg);
        this.getList();
      });
    },
    handleRemark(row){
     this.form.id = row.id;
     this.form.remark = row.remark;
     this.open = true;
    },
    submitRemark(){
      settleRemark(this.form).then(response => {
        this.msgSuccess("数据提交成功");
        this.getList();
        this.open = false;
      });
    },
  }
};
</script>
